package cn.hiapi.admin.api;

import cn.hiapi.admin.query.ActivityHomeAdQuery;
import cn.hiapi.admin.vo.ActivityHomeAdVo;
import cn.hiapi.basic.controller.BasicCurdController;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.ActivityHomeAd;
import cn.hiapi.shopkeeper.entity.activity.ActivityLotteryAd;
import cn.hiapi.shopkeeper.entity.activity.ActivityLotteryItem;
import cn.hiapi.shopkeeper.service.activity.ActivityHomeAdService;
import cn.hiapi.shopkeeper.service.activity.ActivityLotteryAdService;
import cn.hiapi.shopkeeper.service.activity.ActivityLotteryItemService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author AdinZ
 * @date 2023/2/5 0:16
 */
@RestController
@RequestMapping("/manage/activity-home-ad")
public class AdminActivityHomeAdController extends BasicCurdController<ActivityHomeAd, Long, ActivityHomeAdVo, ActivityHomeAdQuery> {

    public AdminActivityHomeAdController(ActivityHomeAdService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }

    @Override
    @Transactional
    public ActivityHomeAd parseData(ActivityHomeAdVo data) {
        if (data.getItems() == null || data.getItems().isEmpty()) throw new BasicException("请设置奖品列表");
        if (data.getItems().size() != 8) throw new BasicException("奖品数量必须是8个");
        if (data.getStartTime() > System.currentTimeMillis()) throw new BasicException("开始时间错误");
        if (data.getEndTime() < System.currentTimeMillis()) throw new BasicException("结束时间错误");
        if (data.getStatus() != 0) throw new BasicException("已开始或结束的不在再次编辑");

        data.getItems().forEach(e -> {
            if (e.getType() == 0) {
                ActivityLotteryAd ad = this.dispatchContext.getServiceOne(ActivityLotteryAdService.class).get(e.getLid());
                ad.setStartTime(data.getStartTime());
                ad.setEndTime(data.getEndTime());
                if (ad == null) throw new BasicException("请认真选择完整的奖品");
            }
        });
        return data.copy(new ActivityHomeAd());
    }

    @Override
    @Transactional
    public void saveCallback(ActivityHomeAdVo vo, ActivityHomeAd data) {
        this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).deleteByAid(data.getId());
        vo.getItems().forEach(e -> e.setAid(data.getId()));
        this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).save(vo.getItems());
    }

    @Override
    public List<ActivityHomeAdVo> toListVo(List<ActivityHomeAd> list, String[] fields) {
        return this.toList(list, fields);
    }

    @Override
    public ActivityHomeAdVo toData(ActivityHomeAd data, String[] fields) {
        ActivityHomeAdVo vo = data.copy(new ActivityHomeAdVo());
        List<ActivityLotteryItem> items = this.dispatchContext.getServiceOne(ActivityLotteryItemService.class).findByAid(data.getId());
        vo.setItems(items);
        return vo;
    }
}
